package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        /*参数检验*/
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        /*检查分页*/
        dto.checkParam();
        /*构建查询条件执行查询*/
        /*查询参数*/
        IPage<AdChannel> pageParam = new Page<>(dto.getPage(), dto.getSize());
        /*查询条件*/
        LambdaQueryWrapper<AdChannel> query = new LambdaQueryWrapper<>();
        /*构建查询条件*/
        /*频道模糊查询*/
        if (StringUtils.isNotBlank(dto.getName())) {
            query.like(AdChannel::getName, dto.getName());
        }
        /*状态*/
        if (dto.getStatus() != null) {
            query.eq(AdChannel::getStatus, dto.getStatus());
        }
        IPage<AdChannel> pageResult = page(pageParam, query);
        /*3封装返回结果*/
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());
        return result;
    }

    /*新增*/
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        /*参数校验*/
        /*if (adChannel == null || StringUtils.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }*/
        /*执行新增*/
        adChannel.setCreatedTime(new Date());
        if (adChannel.getIsDefault()) { //是默认
            //查询原有的默认频道，并更新非默认
            LambdaQueryWrapper<AdChannel> query = new LambdaQueryWrapper<AdChannel>();
            query.eq(AdChannel::getIsDefault, 1);
            AdChannel one = getOne(query);
            /*判断是否存在默认频道*/
            if (one != null) {
                //更新为非默认
                one.setIsDefault(false);
                updateById(one);
            }
        }
        /*用来检测自定义异常
        if (true) {
            CustException.cust(AppHttpCodeEnum.CUSTOM_EXCEPTION);
        }*/
        save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        /*参数校验*/
       /* if (adChannel == null || StringUtils.isBlank(adChannel.getName()) || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }*/
        /*执行修改*/
        AdChannel channel = getById(adChannel.getId());
        if (adChannel.getIsDefault() != null) { //是默认
            //查询原有的默认频道，并更新非默认
            LambdaQueryWrapper<AdChannel> query = new LambdaQueryWrapper<AdChannel>();
            query.eq(AdChannel::getIsDefault, 1);
            AdChannel one = getOne(query);
            /*判断是否存在默认频道*/
            if (one != null) {
                //更新为非默认
                one.setIsDefault(false);
                updateById(one);
            }
        }
        updateById(channel);
        /*返回结果*/

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断当前频道是否存在 和 是否有效
        AdChannel adChannel = getById(id);
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        if (adChannel.getIsDefault()) {// 默认频道
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ISDEFAULT_INVALID);
        }
        // 启用状态下不能删除
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_CHANNEL_ISSTATUS_ERROR);
        }


        //3.删除频道
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }



}
